﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RA_WebSite.Models;

namespace RA_WebSite.Controllers
{
    public class ProvinoController : Controller
    {
        private RAContext db = new RAContext();

        //
        // GET: /Provino/

        public ActionResult Index()
        {
            var provinoes = db.Provinoes.Include(p => p.Step);
            return View(provinoes.ToList());
        }

        //
        // GET: /Provino/Details/5

        public ActionResult Details(int idStep = 0, int id = 0)
        {
            Provino provino = db.Provinoes.Find(id);
            if (provino == null)
            {
                return HttpNotFound();
            }
            ViewBag.stepSelezionato = idStep;
            return View(provino);
        }

        //
        // GET: /Provino/Create

        public ActionResult Create(Step s)
        {
            ViewBag.Step_Id = new SelectList(db.Steps, "Id", "Id");
            ViewBag.StepSelezionatoID = s.Id;
            return View();
        }

        //
        // POST: /Provino/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Provino provino)
        {
            if (ModelState.IsValid)
            {
                db.Provinoes.Add(provino);
                db.SaveChanges();
                return RedirectToAction("ListaProviniStep", new { id = provino.Step_Id });
            }
            ViewBag.Step_Id = new SelectList(db.Steps, "Id", "Id", provino.Step_Id);
            ViewBag.CampioneSelezionatoID = provino.Step_Id;
            var campioneDescrione = (db.Campiones.Where(c => c.Id == provino.Step_Id)).FirstOrDefault();
            return View(provino);
        }

        //
        // GET: /Provino/Edit/5

        public ActionResult Edit(int idStep = 0, int id = 0)
        {
            Provino provino = db.Provinoes.Find(id);
            if (provino == null)
            {
                return HttpNotFound();
            }
            ViewBag.Step_Id = new SelectList(db.Steps, "Id", "Id", provino.Step_Id);
            ViewBag.StepSelezionaId = idStep;
            return View(provino);
        }

        //
        // POST: /Provino/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Provino provino)
        {
            if (ModelState.IsValid)
            {
                db.Entry(provino).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Step_Id = new SelectList(db.Steps, "Id", "Id", provino.Step_Id);
            return View(provino);
        }

        //
        // GET: /Provino/Delete/5

        public ActionResult Delete(int idStep = 0, int id = 0)
        {
            Provino provino = db.Provinoes.Find(id);
            if (provino == null)
            {
                return HttpNotFound();
            }
            ViewBag.StepSelezionaId = idStep;
            return View(provino);
        }

        //
        // POST: /Provino/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var data = (from s in db.Provinoes
                        where s.Id == id
                        select new
                        {
                            id = s.Step_Id
                        }).FirstOrDefault();     

            Provino provino = db.Provinoes.Find(id);
            db.Provinoes.Remove(provino);
            db.SaveChanges();
            return RedirectToAction("ListaProviniStep", new { id = data.id });
        }



        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        // GET: /Step/ListStepCampione/5

        public ActionResult ListaProviniStep(int id = 0)
        {
            var data = db.Provinoes.Where(s => s.Step_Id == id);
            //var StepAssociatoAlPRovini = (from s in db.Steps where s.Id == id select s).FirstOrDefault();

            var stepAssociatoAlProvini = db.Steps.Where(s => s.Id == id).FirstOrDefault();
            ViewBag.StepAssociato = stepAssociatoAlProvini;
            return View(data.ToList());
        }
    }
}